//
//  ReadBomView.swift
//  WalkNovel
//
//  Created by Kevin on 2023/7/14.
//

import UIKit

class ReadBomView: UIView {
    
    lazy var chapterBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.imageView?.contentMode = .scaleAspectFit
        btn.imageEdgeInsets = .init(top: 13, left: 13, bottom: 13, right: 13)
        btn.rx.tap.bind { [weak self] in
            self?.btnClosure?("chapter")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var settingBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.imageView?.contentMode = .scaleAspectFit
        btn.imageEdgeInsets = .init(top: 13, left: 13, bottom: 13, right: 13)
        btn.rx.tap.bind { [weak self] in
            self?.btnClosure?("setting")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var darkBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.imageView?.contentMode = .scaleAspectFit
        btn.imageEdgeInsets = .init(top: 13, left: 13, bottom: 13, right: 13)
        btn.rx.tap.bind { [weak self] in
            self?.btnClosure?("dark")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    lazy var commentBtn: UIButton = {
        let btn = UIButton.init(type: .custom)
        btn.imageView?.contentMode = .scaleAspectFit
        btn.imageEdgeInsets = .init(top: 13, left: 13, bottom: 13, right: 13)
        btn.rx.tap.bind { [weak self] in
            self?.btnClosure?("comment")
        }.disposed(by: rx.disposeBag)
        return btn
    }()
    
    var btnClosure: StringClosure?
    
    override init(frame: CGRect) {
        super.init(frame: frame)
        backgroundColor = .white
        addSubview(chapterBtn)
        addSubview(commentBtn)
        addSubview(settingBtn)
        addSubview(darkBtn)
        let padding = (ScreenW-200)*0.2
        chapterBtn.snp.makeConstraints { (make) in
            make.top.equalTo(settingBtn.snp.top)
            make.left.equalTo(padding)
            make.width.height.equalTo(50)
        }
        commentBtn.snp.makeConstraints { (make) in
            make.top.equalTo(settingBtn.snp.top)
            make.right.equalTo(-padding)
            make.size.equalTo(chapterBtn.snp.size)
        }
        settingBtn.snp.makeConstraints { (make) in
            make.top.equalTo(17)
            make.left.equalTo(chapterBtn.snp.right).offset(padding)
            make.size.equalTo(chapterBtn.snp.size)
        }
        darkBtn.snp.makeConstraints { (make) in
            make.top.equalTo(settingBtn.snp.top)
            make.right.equalTo(commentBtn.snp.left).offset(-padding)
            make.size.equalTo(chapterBtn.snp.size)
        }
        wn_cornerWithBorder(0, 0, .white, 1, .init(white: 0, alpha: 0.1), .init(width: 0, height: 1), 5)
        layer.masksToBounds = false
        darkModeConfig()
    }
    
    func darkModeConfig() {
        backgroundColor = isDark() ? .init(named: "black_121212") : .white
        darkBtn.setImage(UIImage(named: isDark() ? "read_setting_light_max_night" : "read_night"), for: .normal)
        chapterBtn.setImage(isDark() ? UIImage(named: "read_character")?.withTintColor(.init(named: "gray_777777")!, renderingMode: .alwaysOriginal) : UIImage(named: "read_character"), for: .normal)
        
        settingBtn.setImage(isDark() ? UIImage(named: "read_setting")?.withTintColor(.init(named: "gray_777777")!, renderingMode: .alwaysOriginal) : UIImage(named: "read_setting"), for: .normal)
        commentBtn.setImage(isDark() ? UIImage(named: "read_comment")?.withTintColor(.init(named: "gray_777777")!, renderingMode: .alwaysOriginal) : UIImage(named: "read_comment"), for: .normal)
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }
}
